home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 109 / EnigmaAmiga109CD.iso / dalla rivista / amiga.free / diropus4.12b_gpl / dopus_print / printdir.c < prev    next >
C/C++ Source or Header  |  2000-01-27  |  13KB  |  530 lines

  1. /*
  2.  
  3. Directory Opus 4
  4. Original GPL release version 4.12
  5. Copyright 1993-2000 Jonathan Potter
  6.  
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License
  9. as published by the Free Software Foundation; either version 2
  10. of the License, or (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. All users of Directory Opus 4 (including versions distributed
  22. under the GPL) are entitled to upgrade to the latest version of
  23. Directory Opus version 5 at a reduced price. Please see
  24. http://www.gpsoft.com.au for more information.
  25.  
  26. The release of Directory Opus 4 under the GPL in NO WAY affects
  27. the existing commercial status of Directory Opus 5.
  28.  
  29. */
  30.  
  31. #include "print.h"
  32.  
  33. #define PD_SIZE 1
  34. #define PD_DATE 2
  35. #define PD_PROT 4
  36. #define PD_COMM 8
  37. #define PD_TYPE 16
  38.  
  39. #define ENTRY_FILE              -1
  40. #define ENTRY_DEVICE             0
  41. #define ENTRY_DIRECTORY          1
  42. #define ENTRY_CUSTOM           999
  43. #define CUSTOMENTRY_DIRTREE      1
  44. #define CUSTOMENTRY_BUFFERLIST   2
  45. #define CUSTOMENTRY_USER         3
  46.  
  47. enum {
  48.     PRINTDIR_SIZE,
  49.     PRINTDIR_DATE,
  50.     PRINTDIR_PROTECTION,
  51.     PRINTDIR_COMMENT,
  52.     PRINTDIR_FILETYPE,
  53.     PRINTDIR_PRINTER,
  54.     PRINTDIR_FILE,
  55.     PRINTDIR_FILEREQUESTER,
  56.     PRINTDIR_FILESTRING,
  57.     PRINTDIR_PRINT,
  58.     PRINTDIR_EXIT,
  59.  
  60.     PRINTDIR_NUM_GADGETS};
  61.  
  62. struct TagItem
  63.     printdir_size_gadget[]={
  64.         {RO_Type,OBJECT_GADGET},
  65.         {RO_GadgetType,GADGET_CHECK},
  66.         {RO_GadgetID,PRINTDIR_SIZE},
  67.         {RO_Top,1},
  68.         {RO_TopFine,4},
  69.         {RO_LeftFine,20},
  70.         {RO_TextNum,STR_SIZE},
  71.         {RO_TextPos,TEXTPOS_RIGHT},
  72.         {RO_BoolOn,TRUE},
  73.         {RO_ChkCenter,TRUE},
  74.         {TAG_END,0}},
  75.     printdir_date_gadget[]={
  76.         {RO_Type,OBJECT_GADGET},
  77.         {RO_GadgetType,GADGET_CHECK},
  78.         {RO_GadgetID,PRINTDIR_DATE},
  79.         {RO_Top,2},
  80.         {RO_TopFine,9},
  81.         {RO_LeftFine,20},
  82.         {RO_TextNum,STR_DATE},
  83.         {RO_TextPos,TEXTPOS_RIGHT},
  84.         {RO_BoolOn,TRUE},
  85.         {RO_ChkCenter,TRUE},
  86.         {TAG_END,0}},
  87.     printdir_protection_gadget[]={
  88.         {RO_Type,OBJECT_GADGET},
  89.         {RO_GadgetType,GADGET_CHECK},
  90.         {RO_GadgetID,PRINTDIR_PROTECTION},
  91.         {RO_Top,1},
  92.         {RO_TopFine,4},
  93.         {RO_Left,10},
  94.         {RO_LeftFine,71},
  95.         {RO_TextNum,STR_PROTECTION},
  96.         {RO_TextPos,TEXTPOS_RIGHT},
  97.         {RO_BoolOn,TRUE},
  98.         {RO_ChkCenter,TRUE},
  99.         {TAG_END,0}},
  100.     printdir_comment_gadget[]={
  101.         {RO_Type,OBJECT_GADGET},
  102.         {RO_GadgetType,GADGET_CHECK},
  103.         {RO_GadgetID,PRINTDIR_COMMENT},
  104.         {RO_Top,2},
  105.         {RO_TopFine,9},
  106.         {RO_Left,10},
  107.         {RO_LeftFine,71},
  108.         {RO_TextNum,STR_COMMENT},
  109.         {RO_TextPos,TEXTPOS_RIGHT},
  110.         {RO_ChkCenter,TRUE},
  111.         {TAG_END,0}},
  112.     printdir_filetype_gadget[]={
  113.         {RO_Type,OBJECT_GADGET},
  114.         {RO_GadgetType,GADGET_CHECK},
  115.         {RO_GadgetID,PRINTDIR_FILETYPE},
  116.         {RO_Top,3},
  117.         {RO_TopFine,14},
  118.         {RO_LeftFine,20},
  119.         {RO_TextNum,STR_FILETYPE},
  120.         {RO_TextPos,TEXTPOS_RIGHT},
  121.         {RO_ChkCenter,TRUE},
  122.         {TAG_END,0}},
  123.  
  124.     printdir_printer_gadget[]={
  125.         {RO_Type,OBJECT_GADGET},
  126.         {RO_GadgetType,GADGET_RADIO},
  127.         {RO_GadgetID,PRINTDIR_PRINTER},
  128.         {RO_Top,5},
  129.         {RO_TopFine,24},
  130.         {RO_LeftFine,22},
  131.         {RO_TextNum,STR_PRINTER},
  132.         {RO_TextPos,TEXTPOS_RIGHT},
  133.         {RO_BoolOn,TRUE},
  134.         {RO_ChkCenter,TRUE},
  135.         {TAG_END,0}},
  136.     printdir_file_gadget[]={
  137.         {RO_Type,OBJECT_GADGET},
  138.         {RO_GadgetType,GADGET_RADIO},
  139.         {RO_GadgetID,PRINTDIR_FILE},
  140.         {RO_Top,6},
  141.         {RO_TopFine,28},
  142.         {RO_LeftFine,22},
  143.         {RO_TextNum,STR_FILE},
  144.         {RO_TextPos,TEXTPOS_RIGHT},
  145.         {RO_ChkCenter,TRUE},
  146.         {TAG_END,0}},
  147.     printdir_filerequester_gadget[]={
  148.         {RO_Type,OBJECT_GADGET},
  149.         {RO_GadgetType,GADGET_TINYGLASS},
  150.         {RO_GadgetID,PRINTDIR_FILEREQUESTER},
  151.         {RO_Left,8},
  152.         {RO_LeftFine,45},
  153.         {RO_Top,6},
  154.         {RO_TopFine,26},
  155.         {RO_Height,1},
  156.         {RO_HeightFine,4},
  157.         {RO_WidthFine,24},
  158.         {RO_HighRecess,TRUE},
  159.         {TAG_END,0}},
  160.     printdir_filestring_gadget[]={
  161.         {RO_Type,OBJECT_GADGET},
  162.         {RO_GadgetType,GADGET_STRING},
  163.         {RO_GadgetID,PRINTDIR_FILESTRING},
  164.         {RO_Left,8},
  165.         {RO_LeftFine,75},
  166.         {RO_Top,6},
  167.         {RO_TopFine,28},
  168.         {RO_Width,32},
  169.         {RO_WidthFine,-59},
  170.         {RO_Height,1},
  171.         {RO_StringLen,256},
  172.         {TAG_END,0}},
  173.  
  174.     printdir_print_gadget[]={
  175.         {RO_Type,OBJECT_GADGET},
  176.         {RO_GadgetType,GADGET_BOOLEAN},
  177.         {RO_GadgetID,PRINTDIR_PRINT},
  178.         {RO_Top,7},
  179.         {RO_TopFine,34},
  180.         {RO_Width,12},
  181.         {RO_Height,1},
  182.         {RO_HeightFine,4},
  183.         {RO_TextNum,STR_PRINT},
  184.         {RO_TextPos,TEXTPOS_CENTER},
  185.         {RO_HighRecess,TRUE},
  186.         {TAG_END,0}},
  187.     printdir_exit_gadget[]={
  188.         {RO_Type,OBJECT_GADGET},
  189.         {RO_GadgetType,GADGET_BOOLEAN},
  190.         {RO_GadgetID,PRINTDIR_EXIT},
  191.         {RO_Left,28},
  192.         {RO_LeftFine,20},
  193.         {RO_Top,7},
  194.         {RO_TopFine,34},
  195.         {RO_Width,12},
  196.         {RO_Height,1},
  197.         {RO_HeightFine,4},
  198.         {RO_TextNum,STR_EXIT},
  199.         {RO_TextPos,TEXTPOS_CENTER},
  200.         {RO_HighRecess,TRUE},
  201.         {TAG_END,0}},
  202.  
  203.     printdir_print_text[]={
  204.         {RO_Type,OBJECT_TEXT},
  205.         {RO_TopFine,2},
  206.         {RO_TextNum,STR_PRINT_DOT},
  207.         {TAG_END,0}},
  208.  
  209.     printdir_output_text[]={
  210.         {RO_Type,OBJECT_TEXT},
  211.         {RO_Top,4},
  212.         {RO_TopFine,21},
  213.         {RO_TextNum,STR_OUTPUT_TO},
  214.         {TAG_END,0}},
  215.  
  216.     *printdir_gadgets[]={
  217.         printdir_size_gadget,
  218.         printdir_date_gadget,
  219.         printdir_protection_gadget,
  220.         printdir_comment_gadget,
  221.         printdir_filetype_gadget,
  222.         printdir_printer_gadget,
  223.         printdir_file_gadget,
  224.         printdir_filerequester_gadget,
  225.         printdir_filestring_gadget,
  226.         printdir_print_gadget,
  227.         printdir_exit_gadget,
  228.         NULL};
  229.  
  230. printdir_string(struct RequesterBase *,int,char *);
  231.  
  232. void do_printdir(vis,portname,wind)
  233. struct VisInfo *vis;
  234. char *portname;
  235. int wind;
  236. {
  237.     struct RequesterBase printdirreq;
  238.     struct IntuiMessage *msg;
  239.     struct Window *window;
  240.     struct Gadget *gadgets[PRINTDIR_NUM_GADGETS];
  241.     ULONG class;
  242.     USHORT code,gadgetid;
  243.     APTR iaddress;
  244.     struct StringExtend stringex;
  245.     struct Requester busyreq;
  246.     char *printdir_filestring_buffer;
  247.     int a,printflags;
  248.  
  249.     printdirreq.rb_width=40;
  250.     printdirreq.rb_height=8;
  251.     printdirreq.rb_flags=0;
  252.  
  253.     fill_out_req(&printdirreq,vis);
  254.  
  255.     printdirreq.rb_privateflags=0;
  256.     printdirreq.rb_screenname=NULL;
  257.  
  258.     if (printdirreq.rb_screen && !(vis->vi_flags&VISF_BORDERS)) {
  259.         printdirreq.rb_flags|=RBF_STRINGS;
  260.         printdirreq.rb_title=NULL;
  261.     }
  262.     else {
  263.         printdirreq.rb_flags|=RBF_BORDERS|RBF_CLOSEGAD|RBF_STRINGS;
  264.         printdirreq.rb_title=string_table[STR_PRINTDIR];
  265.     }
  266.  
  267.     printdirreq.rb_widthfine=45;
  268.     printdirreq.rb_heightfine=45;
  269.     printdirreq.rb_leftoffset=13;
  270.     printdirreq.rb_topoffset=3;
  271.  
  272.     printdirreq.rb_extend=&stringex;
  273.     printdirreq.rb_idcmpflags=0;
  274.     printdirreq.rb_string_table=string_table;
  275.  
  276.     for (a=0;a<2;a++) {
  277.         stringex.Pens[a]=vis->vi_stringcol[a];
  278.         stringex.ActivePens[a]=vis->vi_activestringcol[a];
  279.     }
  280.     stringex.InitialModes=0;
  281.     stringex.EditHook=NULL;
  282.     stringex.WorkBuffer=NULL;
  283.     for (a=0;a<4;a++) stringex.Reserved[a]=0;
  284.  
  285.     if (!(window=OpenRequester(&printdirreq)) ||
  286.         !(addreqgadgets(&printdirreq,printdir_gadgets,gadgets)) ||
  287.         !(AddRequesterObject(&printdirreq,printdir_print_text)) ||
  288.         !(AddRequesterObject(&printdirreq,printdir_output_text))) {
  289.         CloseRequester(&printdirreq);
  290.         return;
  291.     }
  292.  
  293.     RefreshRequesterObject(&printdirreq,NULL);
  294.  
  295.     printdir_filestring_buffer=
  296.         ((struct StringInfo *)gadgets[PRINTDIR_FILESTRING]->SpecialInfo)->Buffer;
  297.  
  298.     read_printdir_env(&printflags,&a,printdir_filestring_buffer);
  299.     if (!a) {
  300.         gadgets[PRINTDIR_PRINTER]->Flags&=~GFLG_SELECTED;
  301.         gadgets[PRINTDIR_FILE]->Flags|=GFLG_SELECTED;
  302.     }
  303.     for (a=0;a<5;a++) {
  304.         if (printflags&(1<<a)) gadgets[a]->Flags|=GFLG_SELECTED;
  305.         else gadgets[a]->Flags&=~GFLG_SELECTED;
  306.     }
  307.     
  308.     RefreshGList(gadgets[0],window,NULL,-1);
  309.  
  310.     FOREVER {
  311.         while (msg=(struct IntuiMessage *)GetMsg(window->UserPort)) {
  312.             class=msg->Class; code=msg->Code;
  313.             iaddress=msg->IAddress;
  314.             ReplyMsg((struct Message *)msg);
  315.  
  316.             switch (class) {
  317.                 case IDCMP_GADGETDOWN:
  318.                     gadgetid=((struct Gadget *)iaddress)->GadgetID;
  319.  
  320.                     switch (gadgetid) {
  321.  
  322.                         case PRINTDIR_PRINTER:
  323.                             gadgets[PRINTDIR_PRINTER]->Flags|=GFLG_SELECTED;
  324.                             gadgets[PRINTDIR_FILE]->Flags&=~GFLG_SELECTED;
  325.                             RefreshGList(gadgets[PRINTDIR_PRINTER],window,NULL,2);
  326.                             break;
  327.  
  328.                         case PRINTDIR_FILE:
  329.                             gadgets[PRINTDIR_PRINTER]->Flags&=~GFLG_SELECTED;
  330.                             gadgets[PRINTDIR_FILE]->Flags|=GFLG_SELECTED;
  331.                             RefreshGList(gadgets[PRINTDIR_PRINTER],window,NULL,2);
  332.                             break;
  333.                     }
  334.                     break;
  335.  
  336.                 case IDCMP_GADGETUP:
  337.                     gadgetid=((struct Gadget *)iaddress)->GadgetID;
  338.  
  339.                     switch (gadgetid) {
  340.  
  341.                         case PRINTDIR_FILEREQUESTER:
  342.                             if (!(get_file_byrequest(gadgets[PRINTDIR_FILESTRING],window,1)))
  343.                                 break;
  344.                         case PRINTDIR_FILESTRING:
  345.                             RefreshStrGad(gadgets[PRINTDIR_FILESTRING],window);
  346.                             if (gadgets[PRINTDIR_PRINTER]->Flags&GFLG_SELECTED) {
  347.                                 gadgets[PRINTDIR_PRINTER]->Flags&=~GFLG_SELECTED;
  348.                                 gadgets[PRINTDIR_FILE]->Flags|=GFLG_SELECTED;
  349.                                 RefreshGList(gadgets[PRINTDIR_PRINTER],window,NULL,2);
  350.                             }
  351.                             break;
  352.  
  353.                         case PRINTDIR_PRINT:
  354.                             printflags=0;
  355.  
  356.                             for (a=0;a<5;a++) {
  357.                                 if (gadgets[a]->Flags&GFLG_SELECTED) printflags|=1<<a;
  358.                             }
  359.  
  360.                             InitRequester(&busyreq);
  361.                             busyreq.Flags=NOISYREQ;
  362.                             Request(&busyreq,window);
  363.                             SetBusyPointer(window);
  364.  
  365.                             printdirectory(&printdirreq,
  366.                                 portname,printflags,
  367.                                 (gadgets[PRINTDIR_PRINTER]->Flags&GFLG_SELECTED)?"PRT:":
  368.                                     printdir_filestring_buffer,wind);
  369.                             gadgetid=PRINTDIR_EXIT;
  370.  
  371.                             EndRequest(&busyreq,window);
  372.  
  373.                         case PRINTDIR_EXIT:
  374.  
  375.                             code=0x1b;
  376.                             break;
  377.                     }
  378.                     if (gadgetid!=PRINTDIR_EXIT) break;
  379.  
  380.                 case IDCMP_VANILLAKEY:
  381.                     if (code!=0x1b) break;
  382.  
  383.                 case IDCMP_CLOSEWINDOW:
  384.  
  385.                     SetBusyPointer(window);
  386.                     printflags=0;
  387.                     for (a=0;a<5;a++) {
  388.                         if (gadgets[a]->Flags&GFLG_SELECTED) printflags|=1<<a;
  389.                     }
  390.                     save_printdir_env(printflags,
  391.                         (gadgets[PRINTDIR_PRINTER]->Flags&GFLG_SELECTED)?1:0,
  392.                         printdir_filestring_buffer);
  393.  
  394.                     CloseRequester(&printdirreq);
  395.                     return;
  396.             }
  397.         }
  398.         Wait(1<<window->UserPort->mp_SigBit);
  399.     }
  400. }
  401.  
  402. void printdirectory(reqbase,portname,flags,output,wind)
  403. struct RequesterBase *reqbase;
  404. char *portname;
  405. int flags;
  406. char *output;
  407. int wind;
  408. {
  409.     struct PrintDirData pddata;
  410.     char buf[256],pbuf[128];
  411.     int out=0,abort=0;
  412.  
  413.     pddata.win=wind;
  414.     if (!(dopus_message(DOPUSMSG_GETPRINTDIR,(APTR)&pddata,portname)))
  415.         return;
  416.  
  417.     while (!out) {
  418.         if (out=Open(output,MODE_NEWFILE)) break;
  419.         if (!(check_error(reqbase,string_table[STR_UNABLE_TO_OPEN_OUTPUT],0)))
  420.             return;
  421.     }
  422.  
  423.     if (pddata.entry) {
  424.         if (!(printdir_string(reqbase,out,pddata.titlebuf))) {
  425.             Close(out);
  426.             return;
  427.         }
  428.         
  429.         while (pddata.entry) {
  430.             if (check_print_abort(reqbase)) {
  431.                 abort=1;
  432.                 break;
  433.             }
  434.             if (!pddata.onlysel || pddata.entry->selected) {
  435.                 switch (pddata.entry->type) {
  436.                     case ENTRY_CUSTOM:
  437.                         if (pddata.entry->comment &&
  438.                             !(printdir_string(reqbase,out,pddata.entry->comment))) abort=1;
  439.                         break;
  440.                     case ENTRY_DEVICE:
  441.                         lsprintf(buf,"%-32s",pddata.entry->name);
  442.                         if (pddata.entry->comment) strcat(buf,pddata.entry->comment);
  443.                         if (!(printdir_string(reqbase,out,buf))) abort=1;
  444.                         break;
  445.                     default:
  446.                         lsprintf(buf,"%-30s",pddata.entry->name);
  447.                         if (flags&PD_SIZE) {
  448.                             if (pddata.entry->type<=ENTRY_FILE) {
  449.                                 lsprintf(pbuf,"%7ld",pddata.entry->size);
  450.                                 strcat(buf,pbuf);
  451.                             }
  452.                             else strcat(buf,"    Dir");
  453.                         }
  454.                         if (flags&PD_PROT) {
  455.                             strcat(buf," ");
  456.                             strcat(buf,pddata.entry->protbuf);
  457.                         }
  458.                         if (flags&PD_DATE) {
  459.                             strcat(buf," ");
  460.                             strcat(buf,pddata.entry->datebuf);
  461.                         }
  462.                         if (flags&PD_TYPE && pddata.entry->description) {
  463.                             strcat(buf,"(");
  464.                             strcat(buf,pddata.entry->description);
  465.                             strcat(buf,")");
  466.                         }
  467.                         if (!(printdir_string(reqbase,out,buf))) {
  468.                             abort=1;
  469.                             break;
  470.                         }
  471.                         if (flags&PD_COMM && pddata.entry->comment) {
  472.                             StrCombine(buf,": ",pddata.entry->comment,256);
  473.                             if (!(printdir_string(reqbase,out,buf))) abort=1;
  474.                         }
  475.                         break;
  476.                 }
  477.             }
  478.             if (abort) break;
  479.             pddata.entry=pddata.entry->next;
  480.         }
  481.         if (!abort) Write(out,"\n",1);
  482.     }
  483.     Close(out);
  484. }
  485.  
  486. printdir_string(reqbase,file,string)
  487. struct RequesterBase *reqbase;
  488. int file;
  489. char *string;
  490. {
  491.     int len;
  492.  
  493.     FOREVER {
  494.         if ((Write(file,string,(len=strlen(string))))==len) {
  495.             Write(file,"\n",1);
  496.             return(1);
  497.         }
  498.         if (!(check_error(reqbase,string_table[STR_ERROR_PRINTING_DIR],0)))
  499.             return(0);
  500.     }
  501. }
  502.  
  503. void save_printdir_env(flags,printer,filename)
  504. int flags,printer;
  505. char *filename;
  506. {
  507.     int file;
  508.  
  509.     if (file=Open("env:DOpus_printdir.prefs",MODE_NEWFILE)) {
  510.         Write(file,(char *)&flags,sizeof(int));
  511.         Write(file,(char *)&printer,sizeof(int));
  512.         Write(file,filename,strlen(filename)+1);
  513.         Close(file);
  514.     }
  515. }
  516.  
  517. void read_printdir_env(flags,printer,filename)
  518. int *flags,*printer;
  519. char *filename;
  520. {
  521.     int file;
  522.  
  523.     if (file=Open("env:DOpus_printdir.prefs",MODE_OLDFILE)) {
  524.         Read(file,(char *)flags,sizeof(int));
  525.         Read(file,(char *)printer,sizeof(int));
  526.         Read(file,filename,256);
  527.         Close(file);
  528.     }
  529. }
  530.